package com.pyue.eam.app.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pyue.eam.admin.api.except.ApiAsserts;
import com.pyue.eam.app.api.entity.AppArticleEntity;
import com.pyue.eam.app.api.entity.AppPageEntity;
import com.pyue.eam.app.api.entity.AppTabbarEntity;
import com.pyue.eam.app.mapper.AppArticleMapper;
import com.pyue.eam.app.mapper.AppPageMapper;
import com.pyue.eam.app.mapper.AppTabbarMapper;
import com.pyue.eam.app.mapper.AppUserMapper;
import com.pyue.eam.app.service.AppIndexService;
import com.pyue.eam.common.core.constant.CacheConstants;
import com.pyue.eam.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * app 页面控制
 *
 * @author eam
 * @date 2023/6/8
 */

@Service
@RequiredArgsConstructor
public class AppIndexServiceImpl implements AppIndexService {
    private final AppArticleMapper appArticleMapper;
    private final AppTabbarMapper appTabbarMapper;
    private final AppPageMapper appPageMapper;
    private final AppUserMapper appUserMapper;
    private final StringRedisTemplate redisTemplate;

    /**
     * 商城首页 首页数据
     */
    @Override
    public Map<String, Object> index() {
        Map<String, Object> response = new LinkedHashMap<>();
        AppPageEntity appPageEntity = appPageMapper.selectById(1);
        List<AppArticleEntity> articleList = appArticleMapper.selectList(Wrappers.<AppArticleEntity>lambdaQuery().orderByDesc(AppArticleEntity::getSort));
        response.put("pages", appPageEntity);
        response.put("article", articleList);
        return response;
    }

    @Override
    // @Cacheable(value = CacheConstants.GLOBALLY + "app:config", key = "#flag", unless = "#result.size() == 0")
    public Map<String, Object> config(String flag) {
        Long roleTypeId = appUserMapper.getRoleTypeIdByUserId(SecurityUtils.getUserId());
        ApiAsserts.isTrue(roleTypeId != null, "用户类型为空");

        List<AppTabbarEntity> tabbarEntityList = new ArrayList<>();
        if (Boolean.TRUE.equals(redisTemplate.hasKey(CacheConstants.GLOBALLY + "app:config" + roleTypeId))) {
            tabbarEntityList = JSONUtil.toList(redisTemplate.opsForValue().get(CacheConstants.GLOBALLY + "app:config" + roleTypeId), AppTabbarEntity.class);
        }
        if (CollUtil.isEmpty(tabbarEntityList)) {
            tabbarEntityList = getAppTabbarEntities(flag, roleTypeId);
            redisTemplate.opsForValue().set(CacheConstants.GLOBALLY + "app:config" + roleTypeId, JSONUtil.toJsonStr(tabbarEntityList));
        }
        Map<String, Object> response = new LinkedHashMap<>();
        response.put("tabbar", tabbarEntityList);
        return response;
    }

    private List<AppTabbarEntity> getAppTabbarEntities(String flag, Long roleTypeId) {
        List<AppTabbarEntity> tabbarEntityList;
        tabbarEntityList = appTabbarMapper.selectList(Wrappers.lambdaQuery(AppTabbarEntity.class).eq(AppTabbarEntity::getTypeId, Convert.toStr(roleTypeId,
                flag)).orderByAsc(AppTabbarEntity::getOrderNum));
        return tabbarEntityList;
    }

    /**
     * 装修
     *
     * @param pageType 主键
     * @return Map<String, Object>
     */
    @Override
    public AppPageEntity decorate(Integer pageType) {
        Long roleTypeId = appUserMapper.getRoleTypeIdByUserId(SecurityUtils.getUserId());
        ApiAsserts.isTrue(roleTypeId != null, "用户类型为空");
        String decorateKey = CacheConstants.GLOBALLY + "app:decorate:" + pageType + ":" + roleTypeId;
        // if (redisTemplate.hasKey(decorateKey)) {
        //     AppPageEntity appPageEntity = JSONUtil.toBean(redisTemplate.opsForValue().get(decorateKey), AppPageEntity.class);
        //     return appPageEntity;
        // }
        AppPageEntity appPageEntity = appPageMapper.pageTypeAndRoleType(pageType, roleTypeId);
        ApiAsserts.isTrue(appPageEntity != null, "装修页面不存在");

        redisTemplate.opsForValue().set(decorateKey, JSONUtil.toJsonStr(appPageEntity));

        return appPageEntity;
        // return appPageMapper.selectById(id);
    }

}
